package com.linzelin;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.linzelin.easy.DemoData;
import com.linzelin.easy.ExcelUtils;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class EasyTest {
    private String path = "C:\\Users\\啦啦啦\\Desktop\\";
    private List<DemoData> data(){
        List<DemoData> list = new ArrayList<DemoData>();
        for (int i = 0; i < 10; i++) {
            DemoData data = new DemoData();
            data.setString("字符串"+i);
            data.setDate(new Date());
            data.setDoubleData(0.56);
            data.setOther("其他"+i);
            list.add(data);
        }
        return list;
    }

    @Test
    public void simpleWrite(){
        String fileName = path + "EasyTest.xlsx";
        EasyExcel.write(fileName,DemoData.class).sheet("模板").doWrite(data());
        //这里使用的是07版本的excel,要指定对应的class
        //这里会写入到对应的第一个表格中,名称为模板,文件流会自动关闭
    }

    @Test
    public void simpleRead(){
        String path = "C:\\Users\\啦啦啦\\Desktop\\试题模板.xlsx";
       // EasyExcel.read(path, ExcelUtils.class,new ExcelUtils()).sheet().doRead();
        ExcelReader excelReader = null;

        try {

            excelReader = EasyExcel.read(path, ExcelTest.class, new ExcelUtils()).build();

            ReadSheet readSheet = EasyExcel.readSheet(0).build();

            excelReader.read(readSheet);

        } finally {

            if (excelReader != null) {

                // 这里千万别忘记关闭，读的时候会创建临时文件，到时磁盘会崩的

                excelReader.finish();

            }

        }
    }
}
